#!/usr/bin/perl
#
# listmaintainers - write a Web page with all the
# maintainers of the various Linux Counter areas
#
use Counter;
use POSIX;
use Encode;

$date = localtime();

$places = FileTable::open("db/places", O_RONLY);
$users = FileTable::open("db/users", O_RONLY);
$persons = FileTable::open("db/persons", O_RONLY);

# Get all records with maintainers in them
print STDERR "Reading" if -t STDERR;
@placelist = $places->select("maintainer > 0");
for $rec (@placelist) {
    ++ $reccount;
    print STDERR "." if $reccount % 200 == 0 && -t STDERR;
    my $parent = $places->get($rec->{within});
    if ($rec->{maintainer} && $parent->{maintainer} != $rec->{maintainer}) {
	++ $maintainers;
	$maintained{$rec->{name}} = $rec;
    }
}
print STDERR "\n" if -t STDERR;
print STDERR "$reccount records, $maintainers maintainers\n";

print <<EoF;
<?php
 include "counter2.php";

pagestart();
?>
<html>
<?php pagehead(_("Linux Counter Maintainers")) ?>
<body bgcolor="white">
<?php pagetop(_("Linux Counter Maintainers")) ?>
<?php print _("Generated at") ?> $date
<table border=4>
EoF

for $nm (sort(keys(%maintained))) {
    $rec = $maintained{$nm};
    my %maint;
    for $key (split(" ", $rec->{maintainer})) {
	$maint{$key}{user} =     $users->get($key);
	$maint{$key}{person} = $persons->get($key);
    }
    $longname = Encode::encode("utf8", $rec->{longname});
    print "<tr>";
    #print "<td>", $rec->{name}, "</td><td>", $rec->{longname}, "</td>";
    print "<td>", $rec->{name}, "</td>\n<td>", $longname, "</td>\n";
    print "<td nowrap>";
    for $mkey (keys(%maint)) {
	$m = $maint{$mkey};
	if (!$$m{user}) {
	    print $mkey , " - ERROR<br>\n";
	} else {
	    print $mkey, " ", $$m{user}->{login}, "<br>\n";
	}
    }
    print "</td>\n<td nowrap>";
    for $m (values(%maint)) {
	if ($$m{person}) {
            $name = Encode::encode("utf8", $$m{person}->{name});
            #print $$m{person}->{name}, "<br>\n";
            print $name, "<br>\n";
	}
    }
    print "</td>\n<td nowrap>";
    for $m (values(%maint)) {
	if ($$m{person}) {
            $my_email = $$m{person}->{email} ;
            #$my_email =~ s/@/&#x40;/g;
            print "<?php print emailCloaking(\"$my_email\",0) ?><br>\n";
            # Replaced by previous 3 lines 
            # print $$m{person}->{email}, "<br>";
	}
    }
    print "</td>\n</tr>\n";
}


print <<EoF;
</table>
<?php pagebottom() ?>
</body>
</html>
EoF
